<?php

namespace App\Admin\Controllers;

use App\Models\Picture;
use Encore\Admin\Controllers\AdminController;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Show;

class PictureController extends AdminController
{
    /**
     * Title for current resource.
     *
     * @var string
     */
    protected $title = '图片管理';

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        $grid = new Grid(new Picture());
        $grid->disableFilter();
        $type = config('image.type');

        $states = [
            'on'  => ['value' => 'on',  'color' => 'primary'],
            'off' =>  ['value' => 'off',  'color' => 'default'],
        ];
        $grid->disableRowSelector(); //禁用行选择checkbox
        $grid->column('id', __('id'));
        $grid->column('sort', __('排序'))->editable();
        $grid->column('type', __('图片类型'))->filter($type)->label();
//        $grid->column('url')->display(function () {
//            return env('APP_URL').'/uploads/'. $this->value;
//        })->copyable();
        $grid->column('path', '图片')->image('','100','100');

        $grid->column('status', __('状态'))->switch($states);
        $grid->column('created_at', __('创建时间'));
        $grid->column('updated_at', __('更新时间'));

        return $grid;
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     * @return Show
     */
    protected function detail($id)
    {
        $show = new Show(Picture::findOrFail($id));

        $show->field('id', __('id'));
        $show->field('sort', __('排序'));
        $show->field('type', __('图片类型'));
        $show->field('path', __('图片'))->image();
        $show->field('status', __('状态'));
        $show->field('created_at', __('创建时间'));
        $show->field('updated_at', __('更新时间'));

        return $show;
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        $form = new Form(new Picture());
        $status = config('image.status');
        $select = config('image.type');
        $form->select('type', '类型')->options($select);
        $form->hidden('sort', '排序')->default('0');
        $form->image('path', __('图片'))->uniqueName()->removable();
        $form->select('status', '状态')->options($status);
        $form->footer(function ($footer) {
            // 去掉`重置`按钮
            $footer->disableReset();
            // 去掉`查看`checkbox
            $footer->disableViewCheck();
            // 去掉`继续编辑`checkbox
            $footer->disableEditingCheck();
            // 去掉`继续创建`checkbox
            $footer->disableCreatingCheck();
        });
        return $form;
    }
}
